package com.memory.modules.business.service.earn.impl;

import com.memory.common.data.PageSort;
import com.memory.common.exception.ResultException;
import com.memory.modules.business.domain.earn.AccountEarn;
import com.memory.modules.business.domain.user.Account;
import com.memory.modules.business.repository.earn.AccountEarnRecordRepository;
import com.memory.modules.business.repository.earn.AccountEarnRepository;
import com.memory.modules.business.service.earn.AccountEarnService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Optional;

/**
 * @PackageName: com.memory.modules.business.service.earn.impl
 * @ClassName: AccountEarnServiceImpl
 * @Description:
 * @author: zhuguangxun
 * @date 2021/3/13
 */
@Service
@RequiredArgsConstructor
public class AccountEarnServiceImpl implements AccountEarnService {

    private final AccountEarnRepository accountEarnRepository;

    private final AccountEarnRecordRepository accountEarnRecordRepository;

    @Override
    public AccountEarn create(AccountEarn accountEarn) {
        return accountEarnRepository.save(accountEarn);
    }

    @Override
    public AccountEarn modify(Long id, AccountEarn accountEarn) {
        return findById(id).map(e -> {
            accountEarn.setId(id);
            return accountEarnRepository.save(accountEarn);
        }).orElseThrow(() -> new ResultException(500, "未获取信息"));
    }

    @Override
    public AccountEarn remove(Long id) {
        return findById(id).map(e -> {
            accountEarnRepository.delete(e);
            return e;
        }).orElseThrow(() -> new ResultException(500, "未获取信息"));
    }

    @Override
    public Optional<AccountEarn> findById(Long id) {
        return accountEarnRepository.findById(id);
    }

    @Override
    public Page<AccountEarn> findPage(Example<AccountEarn> example) {
        final PageRequest pageRequest = PageSort.pageRequest();
        return accountEarnRepository.findAll(example, pageRequest);
    }

    @Override
    public void saveAccountEarn(Long accountId, Integer count, BigDecimal price, Date createDate) {
        final AccountEarn accountEarn = accountEarnRepository.findAccountEarnByAccountId(accountId);

        if (accountEarn == null) {
            AccountEarn accountEarn1 = new AccountEarn();
            accountEarn1.setAccountId(accountId);
            accountEarn1.setEarnPrice(price);
            accountEarn1.setCount(BigDecimal.valueOf(count));
            accountEarn1.setCreateDate(createDate);
            Assert.notNull(accountEarnRepository.save(accountEarn1), "累加用户收益失败");
        } else {
            accountEarn.setCount(BigDecimal.valueOf(count + accountEarn.getCount().intValue()));
            accountEarn.setEarnPrice(price.add(accountEarn.getEarnPrice()));
            Assert.notNull(accountEarnRepository.save(accountEarn), "新增用户收益失败");
        }
    }

    @Override
    public Optional<AccountEarn> findByAccountId(Long id) {
        return Optional.ofNullable(accountEarnRepository.findAccountEarnByAccountId(id));
    }


}
